﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Celeriq.Common;
using PJB.FacetedNavigation;
using PJBWebsite.Objects;

namespace PJBWebsite
{
    public partial class Default : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            cboDistance.Items.Add(new ListItem("10 miles", "10"));
            cboDistance.Items.Add(new ListItem("20 miles", "20"));
            cboDistance.Items.Add(new ListItem("30 miles", "30"));
            cboDistance.Items.Add(new ListItem("40 miles", "40"));
            cboDistance.Items.Add(new ListItem("50 miles", "50"));
            cboDistance.Items.Add(new ListItem("100 miles", "100"));
            cboDistance.SelectedIndex = 1;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            grdIsland.ItemDataBound += grdIsland_ItemDataBound;
            cmdSearch.Click += cmdSearch_Click;
            cmdZipSearch.Click += cmdZipSearch_Click;

            using (var factory = RepositoryConnection.GetFactory())
            {
                var service = factory.CreateChannel();
                var results = RepositoryConnection.QueryData(new ListingQuery(), service);
                cboState.Items.Add(new ListItem("(All Locations)", string.Empty));
                foreach (var item in results.AllDimensions.StateDimension.RefinementList.OrderBy(x => x.FieldValue))
                {
                    cboState.Items.Add(new ListItem(item.FieldValue, item.DVIdx.ToString()));
                }

                grdIsland.DataSource = results.AllDimensions.SpecialtyDimension.RefinementList
                    .Where(x => x.FieldValue.Length < 14)
                    .OrderBy(x => x.FieldValue)
                    .Take(21);
                grdIsland.DataBind();
            }

        }

        private void cmdZipSearch_Click(object sender, EventArgs e)
        {
            var client = new geolocationservice.GeoLocationServiceClient();
            var info = client.GetZip(txtZip.Text);
            if (info != null)
            {
                var url = new ListingQuery("/results.aspx");
                var radius = Convert.ToInt32(cboDistance.SelectedValue);
                url.FieldFilters.Add(new FieldFilterLocation() { Latitude = info.Latitude, Longitude = info.Longitude, Radius = radius });
                url.NonParsedFieldList.Add(new NamedItem() { Key = "zip", Value = txtZip.Text });
                url.NonParsedFieldList.Add(new NamedItem() { Key = "rd", Value = cboDistance.SelectedValue });
                this.Response.Redirect(url.ToString());
            }
            else
            {
                //Not found
            }
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            var url = new ListingQuery("/results.aspx");
            if (cboState.SelectedIndex > 0)
                url.DimensionValueList.Add(Convert.ToInt32(cboState.SelectedValue));
            if (!string.IsNullOrEmpty(txtSearch.Text))
                url.Keyword = txtSearch.Text;
            this.Response.Redirect(url.ToString());
        }

        private void grdIsland_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var dataItem = e.Item.DataItem as RefinementItem;
                var lblItem = e.Item.FindControl("lblItem") as Literal;
                var url = new ListingQuery("/results.aspx");
                url.DimensionValueList.Add(dataItem.DVIdx);
                lblItem.Text = "<a title=\"" + dataItem.FieldValue + " Jobs (" + dataItem.Count + ")\" href=\"" + url.ToString() + "\">" + dataItem.FieldValue + " Jobs<span class=\"countbubble\">" + dataItem.Count + "</span><a>";
            }
        }
    }
}